package com.yiwei.hdb.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.Spanned;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.allen.library.SuperButton;
import com.yiwei.hdb.R;
import com.yiwei.hdb.api.callback.FundManageTotalContract;
import com.yiwei.hdb.api.requestInfo.FundManageTotalRequest;
import com.yiwei.hdb.constant.Const;
import com.yiwei.hdb.constant.UserGlobal;
import com.yiwei.hdb.event.OnCloseMyfund;
import com.yiwei.hdb.event.OnHomeEventMsg;
import com.yiwei.hdb.model.BankCardListModel;
import com.yiwei.hdb.model.BankListModel;
import com.yiwei.hdb.model.BankModel;
import com.yiwei.hdb.model.MoneyLogModel;
import com.yiwei.hdb.model.MyFundInfoModel;
import com.yiwei.hdb.model.SupportBankModel;
import com.yiwei.hdb.util.PreferenceUtils;
import com.yiwei.hdb.util.Spans;
import com.yiwei.hdb.util.ToastUtil;
import com.yiwei.hdb.util.Util;
import com.yiwei.hdb.view.Dialog;
import com.yiwei.hdb.view.V19FrameLayout;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * Created by Administrator on 2018/6/10.
 */

public class V3MyFundActivity extends BaseActivity implements FundManageTotalContract.View {
    @BindView(R.id.btn_back)
    ImageView btnBack;
    @BindView(R.id.txt_title)
    TextView txtTitle;
    @BindView(R.id.txt_right)
    TextView txtRight;
    @BindView(R.id.layout_title)
    V19FrameLayout layoutTitle;
    @BindView(R.id.img_top)
    ImageView imgTop;
    @BindView(R.id.btn_recharge)
    SuperButton btnRecharge;
    @BindView(R.id.txt_money_recharge)
    TextView txtMoneyRecharge;
    @BindView(R.id.txt_money_earn)
    TextView txtMoneyEarn;
    @BindView(R.id.btn_withdraw)
    SuperButton btnWithdraw;
    @BindView(R.id.btn_loan)
    SuperButton btnLoan;
    @BindView(R.id.txt_balance)
    TextView txtBalance;
    private Unbinder unbinder;
    private FundManageTotalRequest api;
    private float money = 0f;
    private String shouxufei;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_fund_v3);
        initBar();
        unbinder = ButterKnife.bind(this);
        init();
    }

    private void init() {
        txtTitle.setText("我的资金");
        txtRight.setText("资金明细");

        txtRight.setVisibility(View.VISIBLE);
        api = FundManageTotalRequest.create();
        api.attachView(this);

        EventBus.getDefault().register(this);
    }

    @Override
    protected void onResume() {
        super.onResume();
        api.getDealDetailInfo();
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEvent(OnCloseMyfund event) {
        finish();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        api.detachView();
        EventBus.getDefault().unregister(this);
        unbinder.unbind();
        unbinder = null;
    }

    @OnClick({R.id.txt_right, R.id.btn_back, R.id.btn_recharge, R.id.btn_withdraw, R.id.btn_loan})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.txt_right:
                Util.startActivity(this, V3MyFundDetailActivity.class);
                break;
            case R.id.btn_back:
                finish();
                break;
            case R.id.btn_recharge:
                Util.startActivity(this, V3AlipayRechargeActivity.class);
                break;
            case R.id.btn_withdraw:
                if (PreferenceUtils.getString(this, "idcardpassed", "0").equals("0")) {
                    ToastUtil.makeText(this, "请先完成实名");
                    Util.startActivity(this, V3IdcardIdentityActivity.class);
                    return;
                }
                if(UserGlobal.getHasRepayMoney()){
                    Dialog.gotoRepayMoney(this, new Dialog.DialogDefaultClickListener() {
                        @Override
                        public void confirm() {
                            finish();
                        }

                        @Override
                        public void cancel() {

                        }
                    }).show();
                    return;
                }
                api.getBankList();
                break;
            case R.id.btn_loan:
                Map<String, String> map = new HashMap<>();
                map.put("type", Const.SendType.SWITCH_PAGE);
                map.put("currentIndex", 0 + "");
                EventBus.getDefault().post(new OnHomeEventMsg(map));
                Util.startActivity(this, MainActivityNew.class);
                finish();
                break;
        }
    }

    @Override
    public void showError() {

    }

    @Override
    public void complete() {
        loadingDialog.dismiss();
    }

    @Override
    public void onRequestStart() {
        loadingDialog.show();
    }

    @Override
    public void getDealDetailInfoSuccess(MyFundInfoModel item) {
//        MoneyLogModel.User user = item.user;
//        money = Float.valueOf(user.user_balance);
//        txtBalance.setText(user.user_balance);
//        shouxufei = item.user.cash_server_fee;
        if(TextUtils.isEmpty(item.data.money)){
            item.data.money="0";
        }
        txtBalance.setText(item.data.money);

        if(TextUtils.isEmpty(item.data.recharge_money)){
            item.data.recharge_money="0";
        }
        Spanned txt_left = Spans.with(this).font(item.data.recharge_money, getResources().getDimensionPixelSize(R.dimen.x40), 0xff49506D).
                font(  "\n已充值金额(元)", getResources().getDimensionPixelSize(R.dimen.x30), 0xffCACACA).
                build();
        txtMoneyRecharge.setText(txt_left);

        if(TextUtils.isEmpty(item.data.make_money)){
            item.data.make_money="0";
        }
        Spanned txt_right = Spans.with(this).font(item.data.make_money, getResources().getDimensionPixelSize(R.dimen.x40), 0xff49506D).
                font(  "\n已赚取金额(元)", getResources().getDimensionPixelSize(R.dimen.x30), 0xffCACACA).
                build();
        txtMoneyEarn.setText(txt_right);
    }

    @Override
    public void getBankListSuccess(BankCardListModel item) {
        if (item.data.size() <= 0) {
            Dialog.showBankCardAddDialog(this, new Dialog.DialogClickListener() {
                @Override
                public void confirm() {
                    Util.startActivity(V3MyFundActivity.this, V3BankMoreListActivity.class);
                }
            });
        } else {
            Util.startActivity(this, WithdrawV2Activity.class);
        }
    }

    @Override
    public void getBankRealnameSuccess(int type, BankModel item) {

    }

    @Override
    public void getBankRealnameFail(String msg) {

    }
}
